package com.wordcount;

import com.output.LogMapper;
import com.output.LogOutputFormat;
import com.output.LogReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class LogDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        // 获取配置信息和job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 关联本Driver的jar
        job.setJarByClass(LogDriver.class);

        // 关联mapper和reducer
        job.setMapperClass(LogMapper.class);
        job.setReducerClass(LogReducer.class);

        // 设置mapper输出的kv
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);

        // 设置最终输出的kv
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        // 使用自定义outputformat
        job.setOutputFormatClass(LogOutputFormat.class);

        // 设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 应对小文件，可以使用CombineTextInputFormat
        job.setInputFormatClass(CombineFileInputFormat.class);
        // 设置切片的大小
        CombineFileInputFormat.setMaxInputSplitSize(job, 4194304);

        // 提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
